package org.zjsru.user.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cglib.core.Local;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.zjsru.user.pojo.PatternPropterties;
import org.zjsru.user.pojo.User;
import org.zjsru.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.SignatureAlgorithm;
import javax.crypto.SecretKey;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Base64;

@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Autowired
    private UserService userService;
    @Value("${pattern.dateformat}")
    private String dateformat;
    @Autowired
    private PatternPropterties propterties;

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id) throws InterruptedException {
        /*Thread.sleep(6000);*/
        return userService.queryById(id);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Map<String, Object> register(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            User registeredUser = userService.register(user);
            result.put("success", true);
            result.put("message", "注册成功");
            result.put("data", registeredUser);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Map<String, Object> login(@RequestParam String username, @RequestParam String password) {
        Map<String, Object> result = new HashMap<>();
        try {
            String token = userService.login(username, password);
            result.put("success", true);
            result.put("message", "登录成功");
            result.put("token", token);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/{id}")
    public Map<String, Object> updateUser(@PathVariable Long id, @RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            user.setId(id);
            User updatedUser = userService.updateUser(user);
            result.put("success", true);
            result.put("message", "更新成功");
            result.put("data", updatedUser);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 检查用户状态
     */
    @GetMapping("/{id}/status")
    public Map<String, Object> checkUserStatus(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        boolean status = userService.checkUserStatus(id);
        result.put("success", true);
        result.put("status", status);
        result.put("message", status ? "用户正常" : "用户异常");
        return result;
    }

    @GetMapping("/now")
    public String now() {
        return LocalDate.now().format(
                DateTimeFormatter.ofPattern(dateformat, Locale.CHINA)
        );
    }
    @GetMapping("/now2")
    public String now2() {
        return LocalDate.now().format(
                DateTimeFormatter.ofPattern(propterties.getDateformat(), Locale.CHINA)
        );
    }

    @PostMapping("/generate-secret")
    public String generateSecret() {
        SecretKey key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
        return Base64.getEncoder().encodeToString(key.getEncoded());
    }
}
